package com.jsh.erp.service.node;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.datasource.entities.Node;
import com.jsh.erp.datasource.entities.Order;
import com.jsh.erp.datasource.entities.User;
import com.jsh.erp.datasource.mappers.NodeMapper;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.order.OrderService;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @version V1.0
 * @program: jshERP
 * @author: gaoqijun工作节点
 * @create: 2020-11-25 11:18
 * @description:
 **/
@Service
public class NodeService {
    private Logger logger = LoggerFactory.getLogger(NodeService.class);
    @Resource
    private NodeMapper nodeMapper;
    @Resource
    private LogService logService;
    public Node getOrder(long id)throws Exception {
        Node result=null;
        try{
            result=nodeMapper.selectByPrimaryKey(id);
        }catch(Exception e){
            JshException.readFail(logger, e);
        }
        return result;
    }
    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertNode(Node node, HttpServletRequest request)throws Exception {
        //0:未开始，1：加工中，2已结束
        User user = (User) request.getSession().getAttribute("user");
        node.setStatus("0");
        int result=0;
        try{
            result = nodeMapper.insertSelective(node);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(user.getUsername()).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e);
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateNode(Node node, Long id, HttpServletRequest request)throws Exception {
        int result=0;
        User user = (User) request.getSession().getAttribute("user");
        try{
            result = nodeMapper.updateByPrimaryKeySelective(node);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(user.getUsername()).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e);
        }
        return result;
    }
}
